//
//  GuideAlertPageView.swift
//  ClockWidget
//
//  Created by TSYH on 2024/4/2.
//

import UIKit

@objc public protocol GuideAlertPageViewDataSource {
    func contentInset(for pageView: ImageAnimationPageView) -> UIEdgeInsets
    func imageNames(for pageView: ImageAnimationPageView) -> [String]
    func axis(for pageView: ImageAnimationPageView) -> NSLayoutConstraint.Axis
    func rotationAngle(for pageView: ImageAnimationPageView) -> CGFloat
    
    func pageView(_ pageView: ImageAnimationPageView, animationDirectionFor index: Int) -> Int
    func pageView(_ pageView: ImageAnimationPageView, itemSizeFor index: Int) -> CGSize
}

public class ImageAnimationPageView: UIView {
    
    public weak var dataSource: GuideAlertPageViewDataSource?
    
    lazy var contentView = UIView()
    lazy var stackView: UIStackView = {
        let v = UIStackView()
        v.spacing = 16
        v.distribution = .fillEqually
        v.alignment = .fill
        return v
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        contentView.addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public var margin: CGFloat = 12 {
        didSet {
            stackView.spacing = margin
        }
    }
    
    public func reload() {
        guard let dataSource = dataSource else { return }
        stackView.arrangedSubviews.forEach({ $0.removeFromSuperview() })
        
        let axis = dataSource.axis(for: self)
        stackView.axis = axis
        let inset = dataSource.contentInset(for: self)
        stackView.snp.remakeConstraints { make in
            make.top.equalTo(inset.top)
            make.leading.equalTo(inset.left)
            make.trailing.equalTo(-inset.right)
            make.bottom.equalTo(-inset.bottom)
        }
        
        let angle = dataSource.rotationAngle(for: self)
        if angle != 0 {
            contentView.transform = CGAffineTransform(rotationAngle: angle)
        }
        
//        self.backgroundColor = .gray
        let images = dataSource.imageNames(for: self)
        for (index, imageName) in images.enumerated() {
            let itemSize = dataSource.pageView(self, itemSizeFor: index)
            let directionValue = dataSource.pageView(self, animationDirectionFor: index)
            let direction = ImageAnimationRowView.Direction(rawValue: directionValue) ?? .leftToRight
            
            let imageView = ImageAnimationRowView()
            imageView.margin = margin
            imageView.direction = direction
            imageView.set(imageName: imageName, size: itemSize)
            
//            imageView.backgroundColor = .red.withAlphaComponent(0.2*CGFloat(index+1))
            stackView.addArrangedSubview(imageView)
        }
    }
    
    public func startAnimations() {
        stackView.arrangedSubviews.forEach({ ($0 as? ImageAnimationRowView)?.startAnimation() })
    }
}
